package org.luxor.common.kettle.config;

import org.luxor.common.kettle.client.impl.KettleClientImpl;
import org.luxor.common.kettle.config.properties.KettleProperties;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
 * Kettle 全局配置
 *
 * @author Mr.Yan  @date: 2022/04/06
 */
@Configuration
@EnableConfigurationProperties(KettleProperties.class)
@ConditionalOnProperty(prefix = KettleProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class KettleAutoConfig {
    private static final Logger logger = LoggerFactory.getLogger(KettleAutoConfig.class);

    public KettleAutoConfig(KettleProperties kettleProperties) throws KettleException {
        logger.info("Kettle Initialization configuration.  Started...");

        Assert.notNull(kettleProperties, "Spring容器中找不到[kettleProperties]的实例化对象!");
        Assert.notNull(kettleProperties, "Spring容器中找不到[resourceLoader]的实例化对象!");

        // 初始化配置
        logger.info("Kettle Initialization Environment.");
        KettleEnvironment.init();

        logger.info("Kettle Initialization configuration.  completed.");
    }

    @Bean
    @ConditionalOnMissingBean
    public KettleClientImpl kettleService(KettleProperties kettleProperties) {
        return new KettleClientImpl(kettleProperties);
    }

}
